package com.xing.jvmsandbox.weave.test;

import com.xing.jvmsandbox.WeaveManager;
import com.xing.jvmsandbox.event.BeforeEvent;
import com.xing.jvmsandbox.event.Event;
import com.xing.jvmsandbox.listener.EventListener;
import com.xing.jvmsandbox.util.ReflectUtils;
import java.lang.reflect.Method;

public class WeavMain {

    public static void main(String[] args) throws Throwable{
        EventListener eventListener = new EventListener() {
            @Override
            public void onEvent(Event event) throws Throwable {
                if(event instanceof BeforeEvent) {
                    final BeforeEvent beforeEvent = (BeforeEvent) event;
                    final int[] numberArray = (int[]) beforeEvent.argumentArray[0];
                    numberArray[0] = 40;
                    numberArray[1] = 60;
                    System.err.println("修改了参数值");
                }
            }
        };

        Class<?> computerClass = WeaveManager.getSingleton().weave(Computer.class, "sum", eventListener);

        final Method computer$sumMethod = ReflectUtils.unCaughtGetClassDeclaredJavaMethod(computerClass, "sum", int[].class);
        final Object computerObject = computerClass.newInstance();
        final Integer r = ReflectUtils.unCaughtInvokeMethod(computer$sumMethod, computerObject, new int[]{1, 1});

        System.err.println(r);
    }
}
